package com.ops.wechat.core;

import com.ops.uitl.tapestry.AppPage;
import com.ops.uitl.tapestry.PageData;
import org.apache.tapestry.event.PageEvent;

import java.util.Enumeration;

public abstract class WechatPage extends AppPage
{
	public void pageValidate(PageEvent event) {

		PageData pd = null;
		try {
			pd = getPageData();
		} catch (Exception e) {
			log.error(e);
			log.error("获取PageData失败！");
		}
		log.debug(getPageName() + " validate...");


	}


	@Override
	public void pageBeginRender(PageEvent event) {
		try {
			PageData pd = getPageData();
			// 特殊字符过滤
			String queryString = pd.getRequest().getQueryString();
			if(null != queryString){
				String qparam = queryString.toLowerCase();
				String[] paichu = {"'",">","<","%0D","%0A","<script>","vbscript","alert","%3C","%0d%0a"};
				for(int i=0 ; i<paichu.length; i++){
					if(qparam.indexOf(paichu[i]) != -1){
						//发现特殊字符直接跳转到首页面
						log.error("发现特殊字符..."+queryString);
						String path = "http://wap.xj.10086.cn/mh5/wap?service=page/menu.BusinessMenu&listener=initPage&ID=2016062200000971";
						pd.getResponse().sendRedirect(path);
						return;
					}
				}
			}
			Enumeration paramNames = pd.getRequest().getParameterNames();
			String paramName = "";
			String paramVale ="";
			String[] paichu = {"'",">","<","%0D","%0A","<script>","vbscript","alert","%3C","%0d%0a"};
			while (paramNames.hasMoreElements()){
				paramName = (String) paramNames.nextElement();
				paramVale = (String) pd.getRequest().getParameter(paramName);
				log.debug("[param:" +paramName+":"+paramVale+"]" );
				//此处增加针对paramVale跨站脚本过滤
				for(int i=0 ; i<paichu.length; i++){
					if(paramVale.indexOf(paichu[i]) != -1){
						//发现特殊字符直接跳转到首页面
						log.error("发现特殊字符[POST方式]..."+paramVale);
						String path = "http://wap.xj.10086.cn/";
						pd.getResponse().sendRedirect(path);
						return;
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
		super.pageBeginRender(event);
	}

}